package com.example.exam.logInfo.dao;

import com.example.exam.db.BaseDao;
import com.example.exam.db.PageInfoBean;
import com.example.exam.logInfo.model.LogInfoBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LogInfoDao extends BaseDao<LogInfoBean, Integer> {
    @Autowired
    JdbcTemplate jtl;
    @Override
        /**
            * @Description:插入日志SQL语句
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String getAddSql(){
        return "insert into sys_op_log (id, user_id, url, modle_name, method_name, ip, op_time, data_id, status,oper_style,expend ) values (:id, :user_id, :url, :modle_name, :method_name, :ip, :op_time, :data_id, :status, :oper_style, :expend) ";
    }
    /**
     * @Description:查询log页面，页面数据
     * @Param: [pageInfo, beginTime, endTime]
     * @return: com.example.exam.db.PageInfoBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    public PageInfoBean getOpLogList(PageInfoBean pageInfo, String beginTime, String endTime) {

        String sql = "select (select u.show_name from sys_user u where l.user_id = u.id) userName,"
                + "l.id,l.url,l.modle_name,l.method_name,l.ip,l.op_time,l.data_id,l.status,l.oper_style,l.expend  "
                + "from sys_op_log l where l.status = 0";
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        try {
            if(StringUtils.isNotEmpty(beginTime)){
                sql += " and l.op_time >= :startTime ";
                paramsMap.put("startTime", beginTime);
            }
            if(StringUtils.isNotEmpty(endTime)){
                sql += "and l.op_time <= :endTime ";
                paramsMap.put("endTime", endTime);
            }
            sql += " order by op_time desc ";
            //System.out.println("sql:"+sql);
            return super.pageForMap(sql, paramsMap, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
